//
//  LLSideView.m
//  LoveLove
//
//  Created by 肖兰月 on 2019/4/4.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLSideView.h"
#import "LLSideModel.h"

@interface LLSideView ()<UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) LLButton *cover;
@property (strong, nonatomic) UITableView *tableView;
@end

@implementation LLSideView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews {
    
    self.alpha = 0;
    _cover = [LLInitBaseViewTool buttonWithTitle:nil image:nil titleColor:nil font:0];
    _cover.frame = self.bounds;
    _cover.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
    _cover.alpha = 0;
    [self addSubview:_cover];
    [_cover addTarget:self action:@selector(coverBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(-self.width*0.6, 0, self.width*0.6, self.height) style:UITableViewStylePlain];
    _tableView.backgroundColor = LLColor(36, 42, 52);
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableFooterView = UIView.new;
    _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width*0.6, self.height*0.2)];
    [_tableView registerCellByClassName:@"UITableViewCell"];
    _tableView.separatorInset = UIEdgeInsetsZero;
    _tableView.separatorColor = LLColor(49, 55, 63);
    [self addSubview:_tableView];
//    _tableView.backgroundColor = [UIColor whiteColor];
}

- (void)setModels:(NSArray *)models {
    _models = models;
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    }
    cell.textLabel.font = LLRegularFont(16);
    cell.contentView.backgroundColor = LLColor(36, 42, 52);
    LLSideModel *model = self.models[indexPath.row];
    cell.textLabel.text = model.title;
    cell.textLabel.textColor = model.isSelected.integerValue ? LLColor(225, 0, 0) : UIColor.whiteColor;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    for (LLSideModel *model in self.models) {
        model.isSelected = @"0";
    }
    LLSideModel *model = self.models[indexPath.row];
    model.isSelected = @"1";
    [self.tableView reloadData];
    
    [self hide];
    if ([self.delegate respondsToSelector:@selector(sideViewDidClickIndex:)]) {
        [self.delegate sideViewDidClickIndex:indexPath.row];
    }
}

#pragma mark - private method

- (void)show {
    self.hidden = NO;
    [UIView animateWithDuration:0.2 animations:^{
        self.tableView.left = 0;
        self.cover.alpha = 1;
        self.alpha = 1;
    } completion:^(BOOL finished) {
    }];
}

- (void)hide {
    [UIView animateWithDuration:0.2 animations:^{
        self.cover.alpha = 0;
        self.alpha = 0;
        self.tableView.left = -self.width*0.6;
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

- (void)coverBtnClick {
    [self hide];
}

@end
